package com.mx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.mx.mapper.UserMapper;
import com.mx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author MinXu
 * @since 2021/4/19
 * QQ   754647431
 */
@Controller
public class IndexController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping({"/","/index","/index.html"})
    public String index(Model model) {
        model.addAttribute("minxu", "minxu");
        return "index";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String postLogin(HttpSession session, String username, String password) {
        List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("username", "admin").eq("password", "mx"));
        if(users==null||users.size()==0){
            return "redirect:/login?message=登录失败";
        }
        session.setAttribute("user", users.get(0));
        return "redirect:/";
    }
    @RequestMapping("/loginout")
    public String loginout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/";
    }
}
